<template>
  <div class="app-container">
    <center>
        <el-form>
          <el-form-item>
            <el-input v-model="search" placeholder="请输入搜索项目内容" style='width: 50%;'></el-input>
            <el-button type="primary" icon="el-icon-search" circle style='margin-left:10px;' @click="lists()"></el-button>
          </el-form-item>
        </el-form>
      </center>
<!-- 这是一个 表格 -->   
    <el-table
      :data="lists" v-loading.body="listLoading" element-loading-text="拼命加载中" border fit highlight-current-row
      style="width: 97.5%;margin:20px;margin-left:20px;">
      <el-table-column align="center" prop="proId" label="项目编号">
      </el-table-column>
      <el-table-column align="center" prop="userName" label="项目负责人">
      </el-table-column>
      <el-table-column align="center" prop="telePhone" label="负责人电话">
      </el-table-column>
      <el-table-column align="center" prop="proName" label="项目名称">
      </el-table-column>
      <el-table-column align="center" prop="proTime" label="项目时间" width="190">
      </el-table-column>
      <el-table-column align="center" prop="proCost" label="项目成本">
      </el-table-column>
      <el-table-column align="center" prop="proResource" label="项目资源">
      </el-table-column>
      <el-table-column align="center" prop="proApprove" label="项目审批">
      </el-table-column>
      <el-table-column align="center" prop="proContent" label="项目内容">
      </el-table-column>
      <el-table-column align="center" prop="proRemark" label="备注">
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" v-if="hasPerm('project:update')" @click="showUpdate(scope.$index)">审批</el-button>
        </template>
      </el-table-column>
    </el-table>

<!-- 分页器 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="listQuery.pageNum"
      :page-size="listQuery.pageRow"
      layout="sizes, prev, pager, next, jumper"
      :total="totalCount"
      :page-sizes="[10, 20, 50, 100]">
    </el-pagination>

   <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form class="small-space" :model="tempProject" label-position="left" label-width="100px"
               style='width: 600px; margin-left:50px;'>
        <el-form-item label="项目编号" required>
          <el-input type="text" v-model="tempProject.proId" :disabled="true" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="项目负责人" required>
          <el-input type="text" v-model="tempProject.userName" :disabled="true" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="负责人电话" required>
          <el-input type="text" v-model="tempProject.telePhone" :disabled="true" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="项目名称" required>
          <el-input type="text" v-model="tempProject.proName" :disabled="true" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="项目时间" required>
          <el-input type="text" v-model="tempProject.proTime" :disabled="true" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="项目成本" required>
          <el-input type="text" v-model="tempProject.proCost" :disabled="true" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="项目资源" required>
          <el-input type="text" v-model="tempProject.proResource" :disabled="true" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="项目审批" required>
          <el-radio align="center" v-model="tempProject.proApprove" label="通过" style="width:100px" border>通过</el-radio>
          <el-radio align="center" v-model="tempProject.proApprove" label="不通过" style="width:100px" border>不通过</el-radio>
          <p style="color:#848484;">说明:红色权限为对应菜单内的必选权限</p>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="success" @click="updateProject">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        totalCount: 0,
        search: '',
        list: [],//表格的数据
        allPermission: [],
        listLoading: false,//数据加载等待动画
        dialogStatus: 'update',
        dialogFormVisible: false,
        textMap: {
          update: '编辑',
        },
        listQuery: {
          pageNum: 1, //页码
          pageRow: 50, //每页条数
        },
        tempProject: {
          proId: '',
          userName: '',
          telePhone: '',
          proName: '',
          proTime: '',
          proCost: '',
          proResource: '',
          proApprove: '',
          proContent: '',
          proRemark: '',
          permissions: [],
        },
      }
    },
    created() {
      this.getList();
    },
    computed: {
      // 模糊搜索
      lists () {
        const search = this.search
        if (search) {
          // filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。
          // 注意： filter() 不会对空数组进行检测。
          // 注意： filter() 不会改变原始数组。
          return this.list.filter(data => {
            // some() 方法用于检测数组中的元素是否满足指定条件;
            // some() 方法会依次执行数组的每个元素：
            // 如果有一个元素满足条件，则表达式返回true , 剩余的元素不会再执行检测;
            // 如果没有满足条件的元素，则返回false。
            // 注意： some() 不会对空数组进行检测。
            // 注意： some() 不会改变原始数组。
            return Object.keys(data).some(key => {
              // indexOf() 返回某个指定的字符在某个字符串中首次出现的位置，如果没有找到就返回-1；
              // 该方法对大小写敏感！所以之前需要toLowerCase()方法将所有查询到内容变为小写。
              return String(data[key]).toLowerCase().indexOf(search) > -1
            })
          })
        }
        return this.list
      }
    },
    methods: {
      getList() {
        //查询列表
        this.listLoading = true;
        this.api({
          url: "/project/listProject",
          method: "get",
          params: this.listQuery
        }).then(data => {
          this.listLoading = false;
          this.list = data.list;
          this.totalCount = data.totalCount;
        })
      },
      handleSizeChange(val) {
        //改变每页数量
        this.listQuery.pageRow = val
        this.handleFilter();
      },
      handleCurrentChange(val) {
        //改变页码
        this.listQuery.pageNum = val
        this.getList();
      },
      handleFilter() {
        //查询事件
        this.listQuery.pageNum = 1
        this.getList()
      },
      getIndex($index) {
        //表格序号
        return (this.listQuery.pageNum - 1) * this.listQuery.pageRow + $index + 1
      },
      showUpdate($index) {
        let project = this.list[$index];
        this.tempProject.proId = project.proId;
        this.tempProject.userName = project.userName;
        this.tempProject.telePhone = project.telePhone;
        this.tempProject.proName = project.proName;
        this.tempProject.proTime = project.proTime;
        this.tempProject.proCost = project.proCost;
        this.tempProject.proResource = project.proResource;
        this.tempProject.proApprove = project.proApprove;
        this.tempProject.permissions = [];
        this.dialogStatus = "update"
        this.dialogFormVisible = true
      },
      updateProject() {
        this.api({
          url: "/project/updateProject",
          method: "post",
          data: this.tempProject
        }).then(() => {
          this.getList();
          this.dialogFormVisible = false
        })
      },
    }
  }
</script>
<style scoped>
  .requiredPerm {
    color: #ff0e13;
  }
</style>
